package com.example.algorithm.backtrcaking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : zhy
 * @since : 2022/8/24
 * 子集2
 */
public class SubsetPlus {

    List<List<Integer>> listList=new ArrayList<>();
    List<Integer> list=new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        int len=nums.length;
        boolean[]flag=new boolean[len];
        Arrays.sort(nums);
        backTracking(len,nums,flag,0);
        return listList;
    }

    private void backTracking(int len, int[] nums, boolean[] flag,int startIndex) {

        listList.add(new ArrayList<>(list));
        if(startIndex>=len)
        {
            return;
        }
        for(int i=startIndex;i<len;i++)
        {
            if(i>0&&nums[i-1]==nums[i]&&!flag[i-1]) {
                continue;
            }
            flag[i]=true;
            list.add(nums[i]);
            backTracking(len, nums, flag, i+1);
            flag[i]=false;
            list.remove(list.size()-1);
        }
    }

    public static void main(String[] args) {
        SubsetPlus subsetPlus = new SubsetPlus();
        int []can={1,2,2};
        List<List<Integer>> listList = subsetPlus.subsetsWithDup(can);
        listList.stream().forEach(System.out::println);
    }
}
